package name.zxw.rabbitmq.basic;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * @author zhangxinwang
 * @date 2022/6/17 13:56
 */
public class Worker2 {

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        Channel channel = factory.newConnection().createChannel();

        channel.basicQos(5);

        channel.basicConsume("test", false,
                (consumeTag, message) -> {
                    System.out.println("Worker2 before sleep: " + new String(message.getBody()));
                    try {
                        TimeUnit.SECONDS.sleep(15);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println("Worker2 after sleep: " + new String(message.getBody()));

                    channel.basicAck(message.getEnvelope().getDeliveryTag(), false);
                }, (consumerTag) -> {
                    System.out.println("Worker1 error");
                }
        );
    }


}
